package com.nakamachizu.controller.cron;

import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;
import java.util.logging.Logger;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;
import org.slim3.datastore.Datastore;

import com.google.appengine.api.datastore.Key;
import com.nakamachizu.constant.AppConstant;
import com.nakamachizu.meta.LineDataMeta;
import com.nakamachizu.meta.MarkDataMeta;
import com.nakamachizu.meta.MemberDataMeta;

public class MixiGpsController extends Controller {

    private static final Logger log = Logger.getLogger(MixiGpsController.class.getName());
        
    @Override
    public Navigation run() throws Exception {
        
        Calendar now = Calendar.getInstance(TimeZone.getTimeZone("Asia/Tokyo"));
        MarkDataMeta meta = MarkDataMeta.get();
        
        List<Key> list = Datastore.query(meta).filter(meta.expireDate.lessThan(now.getTime())).asKeyList();
        
        LineDataMeta ldm = LineDataMeta.get();
        List<Key> lmList = Datastore.query(ldm).filter(ldm.expireDate.lessThan(now.getTime())).asKeyList();
        if(lmList.size() > 0){
            list.addAll(lmList);
        }
        
        log.info("削除対象は" + list.size() + "件です。");
        
        if(list.size() > 0){
            Datastore.delete(list);
            log.info(list.size() + "件削除しました。");
        }
        
        return null;
    }
}
